
import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/result.dart';
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/components/button_item.dart';
import 'package:huxiaodui/components/gallery.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';

class AlarmMessagePush extends StatefulWidget {
  const AlarmMessagePush({super.key, required this.alarm});
  final AlarmModel alarm;
  @override
  State<AlarmMessagePush> createState() => _AlarmMessagePushState();
}

class _AlarmMessagePushState extends State<AlarmMessagePush> {
  final List<XFile> _image = [];
  final ImagePicker _picker = ImagePicker();
  var controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('信息上传'),
      ),
      body: ListView(
        children: [
          TextField(
            maxLines: null,
            textInputAction: TextInputAction.next,
            controller: controller,
            decoration: const InputDecoration(
              prefixIcon: Icon(Icons.text_snippet),
              hintText: "",
              labelText: "描述",
            ),
          ),
          ButtonItem(
              iconData: Icons.photo_library,
              title: "添加图片",
              onTap: (){
                showModalBottomSheet(
                  context: context,
                  builder: (context) => Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      GestureDetector(
                        child: ListTile(
                          leading: const Icon(Icons.photo_library),
                          title: const Text('拍照'),
                          onTap: () async {
                            XFile? image = await _picker.pickImage(
                                source: ImageSource.camera,
                                imageQuality: 100);
                            Navigator.pop(context);
                            setState(() {
                              _image.add(image!);
                            });
                          },
                        ),
                      ),
                      GestureDetector(
                        child: ListTile(
                          leading: const Icon(Icons.camera_alt),
                          title: const Text('从相册选择'),
                          onTap: () async {
                            XFile? image = await _picker.pickImage(
                                source: ImageSource.gallery,
                                imageQuality: 100);
                            Navigator.pop(context);
                            setState(() {
                              _image.add(image!);
                            });
                          },
                        ),
                      ),
                    ],
                  ),
                );
              },
              rWidget: const Icon(Icons.add),
          ),

          Container(
            padding: const EdgeInsets.all(3),
            child: GalleryComponents(
              galleryItems: _image.map((image) {
                return GalleryItem(
                  onRemove: () {
                    setState(() {
                      _image.remove(image);
                    });
                  },
                  id: image.path,
                  resource: Image.file(
                    File(image.path),
                    width: 200,
                    height: 200,
                    fit: BoxFit.cover,
                  ),
                );
              }).toList(),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: send,
        tooltip: '提交',
        child: const Text("提交"),
      ),
    );
  }

  send() async {
    if (controller.text.isEmpty && _image.isEmpty) {
      EasyLoading.showError("请输入信息");
      return;
    }
    List<MultipartFile> files = [];
    for (XFile val in _image) {
      files.add(await MultipartFile.fromFile(val.path));
    }

    await Api.request(
      isToast: true,
      fileUpload: true,
      url: AlarmUrl.addMessage,
      data: {
        "alarmId": widget.alarm.id,
        "content": controller.text,
        "files": files,
      },
      success: (Result res) {
        Navigator.pop(context, true);
      },
    );
  }
}
